using System;
using System.Collections.Generic;

using GSN.Mail;
using GSN.Senders;
using GSN.Mail.Messages;

using GSN.Rules;

namespace GSN
{
    class Notifier
    {
        private IMailManager _mailManager = null;
        private ISMSSender _sender = null;
        private List<string> prevMsgs = new List<string>();
        private RulesManager rulesManager = null;

        public Notifier() : this(MailManagerFactory.getMailManager(), SMSSenderFactory.getSMSSender())
        {

        }

        public Notifier(Notifier n)
            : this(MailManagerFactory.getMailManager(), SMSSenderFactory.getSMSSender())
        {            
            // copy previous messages to avoid duplicate messages
            prevMsgs.AddRange(n.prevMsgs);
        }

        public Notifier(IMailManager mailManager, ISMSSender sender)
        {
            _mailManager = mailManager;
            _sender = sender;
            rulesManager = new RulesManager();
        }
        


        public void notifyNewMessages()
        {
            Logger.log("Notifier: Applying General Rules");

            if (!rulesManager.applyGenRules())
            {
                Logger.log("Notifier: General Rules failed.");
                return;
            }

            Logger.log("Notifier: Reading Data.");

            Message[] msgs = _mailManager.getData();

            Logger.log("Notifier: Read " + msgs.Length + " Messages.");

            if (msgs == null || msgs.Length == 0)
                return;
            
            Logger.log("Notifier: Starting Sender.");

            _sender.start();

            Logger.log("Notifier: Iterating Messages.");

            try
            {

                foreach (Message msg in msgs)
                {
                    // entry.id - need to check if unique
                    if (!prevMsgs.Contains(msg.messageId))
                    {
                        if (!rulesManager.applyMsgRules(msg))
                        {
                            Logger.log("Message Rules Failed on Msg: " + msg.messageId);                            
                        }
                        else
                        {
                            msg.send(_sender);
                        }

                        prevMsgs.Add(msg.messageId);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                _sender.stop();
            }

            Logger.log("Notifier: Sender Stopped.");
        }

        public override bool Equals(object obj)
        {
            if (obj is Notifier)
            {
                Notifier o = (Notifier)obj;
                return (o._mailManager.Equals(this._mailManager) &&
                                o._sender.Equals(this._sender));
            }
            else
                throw new ArgumentException("Argument is object not a Notifier");
          
        }

        public override int GetHashCode()
        {
            return _mailManager.GetHashCode() + _sender.GetHashCode();
        }
    }
}
